package com.tutou.ikuser.controller;

import com.tutou.ikuser.pojo.dto.LRegisterDTO;
import com.tutou.ikuser.pojo.dto.Result;
import com.tutou.ikuser.pojo.dto.UserDTO;
import com.tutou.ikuser.service.UserService;
import com.tutou.ikuser.util.ValidateCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static com.tutou.ikuser.util.RedisConstants.LOGIN_TOKEN_KEY;

@RestController
@RequestMapping("/login")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/checkCode")
    public void checkCode(HttpServletRequest request, HttpServletResponse response) {
        userService.checkCode(request, response);
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO) {
        return userService.register(userDTO);
    }

    @PostMapping
    public Result login(@RequestBody LRegisterDTO lregisterDTO, HttpServletRequest request) {
        return userService.login(lregisterDTO, request);
    }

    @GetMapping("/logout")
    public Result logout(@RequestHeader("Authorization") String token) {
        return userService.logout(LOGIN_TOKEN_KEY +token);
    }
}
